package cn.longge.service.impl;

import cn.longge.entity.SysArea;
import cn.longge.listener.SysAreaListener;
import cn.longge.mapper.SysAreaMapper;
import cn.longge.service.ISysAreaService;
import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class SysAreaServiceImpl extends BaseServiceImpl<SysArea> implements ISysAreaService {
    @Autowired
    private SysAreaMapper areaMapper;
    @Override
    public PageInfo selectByPage(int page,int pageSize,Map<String, Object> params) {
        PageHelper.startPage(page,pageSize);
        List<SysArea> sysAreas = areaMapper.selectByPage(params);
        return new PageInfo(sysAreas);
    }

    @Override
    public void download(OutputStream outputStream) {
        EasyExcel.write(outputStream,SysArea.class).sheet().doWrite(areaMapper.selectAll());
    }

    @Override
    public void upload(InputStream inputStream) {
        EasyExcel.read(inputStream,SysArea.class,new SysAreaListener(areaMapper)).sheet().doRead();
    }

    @Override
    public int updateByPrimaryKeySelective(SysArea area) {
        int result = super.updateByPrimaryKeySelective(area);
        if(!StringUtils.isEmpty(area.getOldParentIds()) && !StringUtils.isEmpty(area.getParentIds())
                && !area.getOldParentIds().equals(area.getParentIds())){
            result += areaMapper.updateParents(area);
        }
        return result;
    }


}
